package com.holystone.orca.itsm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.holystone.orca.itsm.shiro.ReloadFilterChainService;
import com.holystone.orca.itsm.system.api.SystemServiceApi;
import com.holystone.orca.itsm.system.model.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/")
public class SysController {
    @Reference(version = "${itsm.controller.version}",
            application = "${dubbo.application.id}",
            check = false,
            url = "dubbo://localhost:12345")
    private SystemServiceApi serviceApi;

    @Autowired
    private ReloadFilterChainService reloadFilterChainService;

    @ResponseBody
    @RequestMapping("user/pagingSysUser")
    public ModelMap pagingSysUser(SysUser user, Integer pageNO, Integer pageSize){
        ModelMap map = new ModelMap();
        map.put("data",serviceApi.pagingSysUser(user,pageNO,pageSize));
        return map;
    }

    @ResponseBody
    @RequestMapping("manager")
    public ModelMap manager() {
        ModelMap map = new ModelMap();
        map.put("msg","有/sys/manager权限");
        return map;
    }

    @ResponseBody
    @RequestMapping("user/addUser")
    public ModelMap addUser(SysUser user) {
        ModelMap map = new ModelMap();
        map.put("user",serviceApi.addSysUser(user));
        return map;
    }
    @ResponseBody
    @RequestMapping("user/getUser")
    public ModelMap getUser(Integer userId) {
        ModelMap map = new ModelMap();
        map.put("data",serviceApi.getUserMsg(userId));
        map.put("code",200);
        return map;
    }
    // 登出
    @ResponseBody
    @RequestMapping(value = "logout")
    public ModelMap logout() {
        ModelMap resultMap = new ModelMap();
        System.out.println("退出登录");
        try {
            // 退出
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        resultMap.put("code",200);
        return resultMap;
    }
    @RequestMapping("updateFilter")
    public String updateFilter(){
        reloadFilterChainService.reloadFilterChains();
        return "权限已更改";
    }
}
